package com.zmn.plat.admin.controller.product.relation;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.product.ServProductRelationBService;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.product.relation.ServProductRelationOperatorVO;

/**
 * @author tanbiao
 * @description:产品关联
 * @since 2019/9/20 11:12
 * @modifier
 **/
@RestController
@RequestMapping("/showProduct/relation/")
public class ServProductRelationModifyController extends PlatBaseController {
    private Logger logger = LoggerFactory.getLogger(ServProductRelationModifyController.class);
    @Resource
    ServProductRelationBService servProductRelationBService;

    /**
     * 功能描述:关联后台产品列表
     *
     * @param ids
     * @return org.springframework.web.servlet.ModelAndView
     * @author tanbiao
     * @since 2019/9/20
     * @modifier
     */
    @RequestMapping(value = "deleteRelations")
    @RequiresPermissions("/showProduct/relation/deleteRelations.action")
    public ResponseDTO deleteRelations(@RequestParam(value = "ids") String ids,
        @RequestParam(value = "showProductId") Integer showProductId) {
        List<Integer> idList = StringUtil.convertStrsToList(ids);
        try {
            servProductRelationBService.deleteBatch(idList, showProductId);
            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "删除数据成功！");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return ResponseDTO.fail("操作失败！");
    }

    /**
     * 功能描述 :批量添加关联关系
     *
     * @param request
     * @param vo
     * @return com.zmn.common.dto.ResultDTO
     * @author tanbiao
     * @since 2019/10/15
     * @modifier
     */
    @RequestMapping(value = "batchAddRelation")
    @RequiresPermissions("/showProduct/relation/addEditView.action")
    public ResponseDTO deleteRelations(HttpServletRequest request, @RequestBody ServProductRelationOperatorVO vo) {
        if (null == vo) {
            return ResponseDTO.fail("请检查参数是否正确！");
        }
        vo.setOperator(getMcStaffRealName());

        try {
            servProductRelationBService.updateRelations(vo);
            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "绑定关系成功！");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return ResponseDTO.fail("操作失败！");
    }
}
